<?php

namespace common\services;

use common\helpers\RsaHelper;
use Yii;
use yii\web\Response;

class ApiSignService
{

    /**
     * 获取公钥文件
     * @return false|string
     */
    public static function getPublicKey($path = '')
    {
        try {
            $path = Yii::getAlias($path ?: Yii::$app->params['api_rsa_public_key']);
            if (!file_exists($path)) {
                throw new \Exception('api模块公钥文件丢失', 404);
            }
            return file_get_contents($path);
        } catch (\Exception $e) {
            Yii::$app->response->format = Response::FORMAT_JSON;
            Yii::$app->response->data = [
                'status' => $e->getCode(),
                'message' => $e->getMessage() . ' at file ' . __FILE__,
            ];
        }

    }

    /**
     * 获取私钥文件
     * @return false|string
     */
    public static function getPrivateKey($path = '')
    {
        try {
            $path = Yii::getAlias($path ?: Yii::$app->params['api_rsa_private_key']);
            if (!file_exists($path)) {
                throw new \Exception('api模块私钥文件丢失', 404);
            }
            return file_get_contents($path);
        } catch (\Exception $e) {
            Yii::$app->response->format = Response::FORMAT_JSON;
            Yii::$app->response->data = [
                'status' => $e->getCode(),
                'message' => $e->getMessage() . ' at file ' . __FILE__,
            ];
        }
    }


    /**
     * 获取签名
     * @param array $data
     * @param string $privateKeyPath
     * @return string
     */
    public static function sign(array $data, $privateKeyPath = '')
    {
        //recursive_key_sort($data);
        ksort($data);
        $encodeData = rsa_encode($data);
        //获取私钥文件
        $privateKey = self::getPrivateKey($privateKeyPath);
        $sign = RsaHelper::sign($encodeData, $privateKey);
        return base64_encode($sign);
    }


    /**
     * 验证签名
     * @param array $data
     * @param string $publicKeyPath
     * @return bool
     */
    public static function verify(array $data, $publicKeyPath = '')
    {

        try {
            if (empty($data['sign'])) {
                throw new \Exception('缺少签名信息sign', '404');
            }

            //获取签名数据并转码
            $sign = base64_decode($data['sign']);
            unset($data['sign']);
            if (empty($data)) {
                throw new \Exception('缺少参数信息', '404');
            }
            //recursive_key_sort($data);
            ksort($data);
            $encodeData = rsa_encode($data);
            //var_dump($encodeData);die;
            $publicKey = self::getPublicKey($publicKeyPath);

            return (bool)RsaHelper::verify($encodeData, $sign, $publicKey);
        } catch (\Exception $e) {
            Yii::$app->response->format = Response::FORMAT_JSON;
            Yii::$app->response->data = [
                'status' => $e->getCode(),
                'message' => $e->getMessage() . ' at file ' . __FILE__,
            ];
            return false;
        }

    }

}